package com.glory.brepda.activity.mfg;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.BinInfoListAdapter;
import com.glory.brepda.adapter.FTBinListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.SimpleEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: FTSampleTrackOutActivity
 * Author: XJH
 * Date: 2020/5/21 17:41
 * Description:FT样品出站
 * History:
 */
public class FTSampleTrackOutActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvEqpMainWork)
    TextView tvEqpMainWork;
    @BindView(R.id.tvEqpItemWork)
    TextView tvEqpItemWork;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvStepName)
    TextView tvStepName;
    @BindView(R.id.tvNeedSample)
    TextView tvNeedSample;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvBinList)
    RecyclerView rvBinList;

    private FTBinListAdapter mBinInfoListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_track_out_sample_ft;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvBinList,this);
        mBinInfoListAdapter = new FTBinListAdapter(null);
        mBinInfoListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mBinInfoListAdapter.isFirstOnly(true);
        rvBinList.setAdapter(mBinInfoListAdapter);
        initEdit(etLot, s -> requestData(s, "Sample",false));
    }

    private void requestData(String lotID, String actionType,boolean isSubmit) {
        FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    SimpleEntity entity = response.body().getResponse().getBody();
                    if (!entity.getTRACKSHOWMSG().equals("")){
                        FreeApi_DialogUtils.getConfirmDialog(FTSampleTrackOutActivity.this, "確定", entity.getTRACKSHOWMSG(), () -> {
                            if (isSubmit){
                                showCustomDialog(entity);
                            }else {
                                tvEqpMainWork.setText(entity.getLOT().getEQUIPMENTID());
                                tvEqpItemWork.setText(entity.getLISTEQUIPMENT().get(0).getEQUIPMENTID());
                                tvLotNum.setText(String.valueOf(entity.getLOT().getMAINQTY()));
                                tvNeedSample.setText(entity.getLOT().getUDF().getNeedSample());
                                tvPartName.setText(entity.getLOT().getPARTNAME());
                                tvStepName.setText(entity.getLOT().getSTEPNAME());
                                mBinInfoListAdapter.getSelectorData().clear();
                                mBinInfoListAdapter.setIndex(-1);
                                mBinInfoListAdapter.setNewData(entity.getLOTBINDATALIST());
                            }
                            return;
                        }).show(getSupportFragmentManager(),"MSG_DIALOG");
                    }
                    if (isSubmit){
                        showCustomDialog(entity);
                    }else {
                        tvEqpMainWork.setText(entity.getLOT().getEQUIPMENTID());
                        tvEqpItemWork.setText(entity.getLISTEQUIPMENT().size()>0?entity.getLISTEQUIPMENT().get(0).getEQUIPMENTID():"");
                        tvLotNum.setText(String.valueOf(entity.getLOT().getMAINQTY()));
                        tvNeedSample.setText(entity.getLOT().getUDF().getNeedSample());
                        tvPartName.setText(entity.getLOT().getPARTNAME());
                        tvStepName.setText(entity.getLOT().getSTEPNAME());
                        mBinInfoListAdapter.getSelectorData().clear();
                        mBinInfoListAdapter.setIndex(-1);
                        mBinInfoListAdapter.setNewData(entity.getLOTBINDATALIST());
                    }
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKOUT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", actionType);
        map.addProperty("EQUIPMENTID", tvEqpMainWork.getText().toString());
        List<LotList> lotList = new ArrayList<>();
        lotList.add(new LotList(lotID));
        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
        map.add("LOTBINDATALIST", new JsonParser().parse(new Gson().toJson(mBinInfoListAdapter.getSelectorData())).getAsJsonArray());
        NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTSampleTrackOutActivity.this, map, callBack);
    }

    private void showCustomDialog(SimpleEntity entity) {
        FreeUI_GeneralFragmentDialog freeU_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_trackout_ft,null);
        TextView tvPartName = view.findViewById(R.id.tvPartName);
        tvPartName.setText(entity.getLOT().getPARTNAME());
        TextView tvLotNum = view.findViewById(R.id.tvLotNum);
        tvLotNum.setText(entity.getLOT().getLOTID());
        TextView tvMainQTY = view.findViewById(R.id.tvMainQTY);
        tvMainQTY.setText(String.valueOf(entity.getLOT().getMAINQTY()));
        TextView tvStates = view.findViewById(R.id.tvStates);
        tvStates.setText(entity.getLOT().getSTATE());
        TextView tvStepName = view.findViewById(R.id.tvStepName);
        tvStepName.setText(entity.getLOT().getSTEPNAME());
        TextView tvStepDesc = view.findViewById(R.id.tvStepDesc);
        tvStepDesc.setText(entity.getLOT().getSTEPDESC());
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        tvConfirm.setOnClickListener(v->{
            finish();
        });

        freeU_generalFragmentDialog.setLayout(view);
        freeU_generalFragmentDialog.show(getSupportFragmentManager(), "SUBMIT_DIALOG");
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
//        if (actionControl("Submit06")) return;

        if (mBinInfoListAdapter.getSelectorData().size() <=0){
            showToast("请先勾选BIN列表",false);
            return;
        }
        if (getText(etLot).equals("")){
            showToast("请先扫描批次号",false);
        }else {
            showSubmitDialog(() -> requestData(getText(etLot),"SampleTrackout",true));
        }
    }
}
